import React, { FC, useMemo, useState } from 'react';
import { useRequest } from 'ahooks';
import useBack from '../../FundCard/hook/useBack';
import { FundsObj } from '../../../type';
import useBaseFunDate from '../../FundCard/hook/useBaseFunDate';
import { Tag } from 'antd-mobile';
import './index.css';
import { zongnum } from '../../cout';

interface CardProps {
  fundInfo: FundsObj;
}

const specialCode = ['004475', '006624', '006195'];

const Card: FC<CardProps> = ({ fundInfo }) => {
  console.log('fundInfo: ', fundInfo);
  const { maxValObj, curValObj, buyObj, setBuyDate } = useBaseFunDate(fundInfo);
  console.log('maxValObj: ', maxValObj);

  const { maxBack, meinianBack } = useBack(fundInfo.code);
  console.log('meinianBack', meinianBack);

  //预估值回测率
  const estimateBack = useMemo(() => {
    /**
     * 往前的最大净值减去现在的预估净值，再除于当时的净值得到回测值。如果没有预估净值则使用现在净值
     * 由于近一，近二，近三都是0-100维度的，所以要乘100
     */
    const drawdownRate =
      ((maxValObj.val - (fundInfo?.expectWorth ?? fundInfo?.netWorth)) / maxValObj.val) * 100;
    console.log('drawdownRate: ', drawdownRate);

    return drawdownRate;
  }, [maxValObj.val, curValObj.val]);

  //回测值相除得到百分比，然后转成百分位
  const getBack = (num?: number) => {
    return (estimateBack / (num ?? 0)) * 100;
  };

  const one = getBack(meinianBack?.yi);
  const two = getBack(meinianBack?.er);
  const third = getBack(meinianBack?.san);

  //根据公式算出该有的概率
  const percentage = useMemo(() => {
    return (one * 1 + two * 2 + third * 4) / ((1 + 2 + 4) * 100);
  }, [meinianBack]);

  console.log('percentage: ', percentage);

  const isSpecial = specialCode.includes(fundInfo.code);
  const isShow = true || percentage > 0.466 || isSpecial;
  const chushi = 35000;
  const initAmount = isSpecial ? chushi * 3 : chushi;
  // 30000 * 4 : 30000   --->360000
  // 30000 * 3 : 30000 ----> 320000
  // 35000 * 3.5 : 35000: 408453.13071365457
  // 35000 * 3 : 35000; 379803.55612894957
  if (meinianBack && isShow) {
    if (!zongnum.arr.includes(fundInfo.code)) {
      zongnum.arr.push(fundInfo.code);
      zongnum.num = zongnum.num + percentage * initAmount;
      console.log('zongnum.num: ', zongnum.num);
    }

    return (
      <div className="count-card">
        {/* {isSpecial ? (
          <Tag color="primary" fill="outline">
            {fundInfo.name}（{fundInfo.code}）
          </Tag>
        ) : (
          <Tag color="default">
            {fundInfo.name}（{fundInfo.code}）
          </Tag>
        )} */}

        <Tag color="default">
          {fundInfo.name}（{fundInfo.code}）
        </Tag>
        <div>
          <Tag color="success">近一 {one.toFixed(2) + '%'}</Tag>
          <Tag color="warning">近两 {two.toFixed(2) + '%'}</Tag>
          <Tag color="danger">近三 {third.toFixed(2) + '%'}</Tag>
          {/* <Tag color="blue">
            应有
            {Math.round(percentage * initAmount)}
          </Tag> */}
        </div>
      </div>
    );
  }

  return <></>;
};
export default Card;
